home *** CD-ROM | disk | FTP | other *** search
/ Amiga Packmags / NewsFlash - Issue 07 (1990-01)(UGA - 17-Bit Software)(Disk 1 of 2)[a].zip / NewsFlash - Issue 07 (1990-01)(UGA - 17-Bit Software)(Disk 1 of 2)[a].adf / sources / tp.c < prev   
C/C++ Source or Header  |  1988-01-19  |  10KB  |  228 lines

  1. /* TP(L) V0.9 - asciifile displayer
  2.    Demo for UGA newsletter
  3.    (c)1989 by Trihedral systems for the Amiga
  4.    Author: Jeroen K Sparla
  5.    Language: Manx Aztec C V3.6a
  6.    Compiler notes: Use option +L for automatic long ints.
  7. */
  8.  
  9. /* place here your includes (outside the comment...)
  10.    I'm using the Aztec +i option for pre-compiled include files.
  11.    you need at least the following headerfiles :
  12.    #include <stdio.h>
  13.    #include <intuition/intuitionbase.h>
  14.    #include <exec/types.h>
  15.    #include <exec/execbase.h>
  16.    #include <exec/exec.h>
  17.    #include <graphics/gfxbase.h>
  18.    #include <graphics/gfx.h>
  19.    #include <graphics/text.h>
  20. */
  21.  
  22. #define GTX 560  /* Emulated Gadget coordinates topleft & bottomright */
  23. #define GTY 0
  24. #define GBX 579
  25. #define GBY 9
  26.  
  27. extern struct Screen *OpenScreen();           /* prototyping for Aztec */
  28.  
  29. LONG IntuitionBase=0L;                        /* pointer to intuition.library */
  30. LONG GfxBase=0L;                              /* pointer to graphics.library */
  31.  
  32. /* our own screen definitions */
  33. struct NewScreen ns= { 0,0,                   /* top x,y */
  34.                        640,200,1,             /* lower x,y and number of bitplanes */
  35.                        0,1,                   /* detail and block pen */
  36.                        HIRES,                 /* we want a hires screen */
  37.                        CUSTOMSCREEN,          /* not the workbench but our own */
  38.                        NULL,                  /* default font */
  39.                        /* the title */
  40.                        (UBYTE *)"TP(L) V0.9 (c)1989 by Trihedral Systems  -=[YEP]=-",
  41.                        NULL                   /* always null, no screengadgets */
  42.                      };
  43.  
  44. struct RastPort *rp=NULL;                     /* pointer to rasterport */
  45. struct Screen *screen=NULL;                   /* pointer to screen */
  46. FILE  *infile=NULL;                           /* pointer to our ascii textfile */
  47. int error=0;                                  /* errornumber during init */
  48. int new=FALSE;                                /* user want to start again ? */
  49. int scndscr=FALSE;                            /* are we past the first screen ? */
  50. int BottomLine;                               /* lowest scanline we want to reach */
  51.  
  52. /*------------------ Initialize the system --------------------------*/
  53.  
  54. void Init()
  55. { /* try to open the graphics library */
  56.   GfxBase=OpenLibrary("graphics.library",0);
  57.   if(GfxBase==NULL)
  58.   { puts("Can't open graphics.library");
  59.     error=10;
  60.   }
  61.   /* try to open the intuition library */
  62.   IntuitionBase=OpenLibrary("intuition.library",0);
  63.   if(IntuitionBase==NULL)
  64.   { puts("Can't open intuition.library");
  65.     error=20;
  66.   }
  67.   /* try to open the new screen */
  68.   screen=OpenScreen(&ns);
  69.   if(screen==NULL)
  70.   { puts("Can't open screen");
  71.     error=30;
  72.   }
  73.   /* place screen in front (should be done automaticly.. just in case */
  74.   ScreenToFront(screen);
  75.   /* initialise pointer to the rasterport from the screen */
  76.   rp = &screen->RastPort;
  77.   /* set the drawing pen to the second color (0,1,2...) of the palette */
  78.   SetAPen(rp,1);
  79.   ShowTitle(screen,FALSE);        /* disable screen title */
  80. }
  81.  
  82. /*------------------- Check if left mousebutton is pressed --------------*/
  83.  
  84. int LeftMouseButton()
  85. { APTR address=0xbfe000;          /* CIAA, bit 6, 1:released, 0:pressed */ 
  86.                                   /* this should be address 0xbfe001, but */
  87.                                   /* we work on word (2byte) boundaries */
  88.   unsigned short Mouse;  
  89.   Mouse=(unsigned short)(*address);        /* fetch data CIAA */
  90.   if((Mouse & 64)==64)                     /* bit 6 set ? */
  91.     return(FALSE);                         /* yes, button not pressed */
  92.   else
  93.     return(TRUE);                          /* no, button is pressed */
  94. }
  95.  
  96. /*------------ Draw an emulated -non intuition- gadget in screentitle ---*/
  97.  
  98. void DrawGadget()
  99. { SetAPen(rp,0);                           /* set drawpen to backgroundcolor */
  100.   RectFill(rp,GTX,GTY,GBX,GBY);            /* draw filled box */
  101.   SetAPen(rp,1);                           /* set drawpen to cursorcolor */
  102.   Move(rp,GTX+4,GTY+2);                    /* move to topleftedge */
  103.   Draw(rp,GBX-4,GTY+2);                    /* draw interior box */
  104.   Draw(rp,GBX-4,GBY-2);
  105.   Draw(rp,GTX+4,GBY-2);
  106.   Draw(rp,GTX+4,GTY+2);
  107. }
  108.  
  109. /*--------------------------- Clear the screen (without titlebar) -------*/
  110.  
  111. CLS()
  112. { SetAPen(rp,0);                           /* background color */
  113.   /* fill until scanline 200 if NTSC, in PAL until line 256 */
  114.   RectFill(rp,0,10,639,BottomLine==201 ? 199 : 255);
  115.   SetAPen(rp,1);                           /* back to cursor color */
  116.   DrawGadget();                            /* place the gadget, just in case... */
  117. }
  118.  
  119. /*----------------- Display the next screen ----------------------------*/
  120.  
  121. NextScreen()
  122. { int x,y,hit=FALSE;                /* mouse x,y position and mouseleftbutton */
  123.   new=FALSE;                        /* does the user want to restart */
  124.   while((!hit) && (!new))           /* user didn't take any action */
  125.   { hit=LeftMouseButton();          /* left mousebutton ? */
  126.     x=screen->MouseX;               /* fetch mouse coordinates */
  127.     y=screen->MouseY;
  128.     /* if the pointer is in our gadget and we are at least at the secondscreen */
  129.     if(x>GTX && x<GBX && y>GTY && y<GBY && scndscr)
  130.       new=TRUE;                     /* the user wants to restart */
  131.   }
  132.   CLS();                            /* always clear the screen */
  133.   if(hit && new)                    /* user in gadget and mousebutton pressed */
  134.     error=(90);                     /* user wants to quit */
  135.   if(hit)                           /* mousebutton pressed ? */
  136.     scndscr=TRUE;                   /* this means we are reading the next screen */
  137. }
  138.  
  139. /*---------- Cleanup the mess we made ------------------------------*/
  140.  
  141. CleanUp()
  142. {
  143.   if(infile) fclose(infile);                     /* close the textfile */
  144.   if(screen) CloseScreen(screen);                /* give screen back.. */
  145.   if(IntuitionBase) CloseLibrary(IntuitionBase); /* don't need this libs anymore */
  146.   if(GfxBase) CloseLibrary(GfxBase);
  147. }
  148.  
  149. /*------- ^C-pressed routine --------------------------------------*/
  150.  
  151. _abort()
  152. { puts("-=INTERRUPTED=- user abort request");
  153.   /* user wants to quit by pressing [ctrl]C on keyboard */
  154.   CleanUp();        /* cleanup the system before */
  155.   exit(100);        /* exit with errornumber */
  156. }
  157.  
  158. /*---------------- main entry routine -----------------------------*/
  159.  
  160. main(argc,argv)
  161. int argc;                        /* urgument count (1..n) */
  162. char *argv[];                    /* the values (0..n-1) */
  163. { int xpos,ypos,len;             /* current x,y cursor position & length */        
  164.   char str[81];                  /* from the string stored int str */
  165.   char ch;                       /* dummy character */
  166.  
  167.   if(argc!=2)                    /* more or less than 1 argument ? */ 
  168.   { puts("USAGE : TP(L) <ascii-file>");        /* display help */
  169.     CleanUp();                   /* cleanup and exit with error */
  170.     error=40;
  171.     goto Quit;
  172.   }
  173.   infile=fopen(argv[1],"r");     /* try to open the specified file */
  174.   if(infile==NULL)               /* nop, not found... */
  175.   { puts("Can't open specified file");
  176.     CleanUp();
  177.     error=50;
  178.     goto Quit;
  179.   }
  180.   /* this is kinda tricky:
  181.   since we have NTSC (200 scanlines) and PAL (256 scanlines) version Amy's
  182.   the name of the command defines the version you want to use. 
  183.   if you name the program :
  184.   TP  you are requesting the NTSC version.
  185.   TPL you are requesting the PAL version.
  186.   */
  187.   BottomLine=201;                /* standard NTSC */
  188.   if(toupper(argv[0][2])=='L')   /* if command's 3th character is an L */
  189.   { BottomLine=257;              /* we want the large screen */
  190.     ns.Height=256;               /* redefine screen height */
  191.   }
  192.   Init();                        /* initialise the system */
  193.   if(error)                      /* error returned ? */
  194.     goto Quit;                        
  195.   CLS();                         /* clear the screen */
  196.   xpos=0; ypos=17;               /* cursor position */
  197.   Move(rp,xpos,ypos);            /* go set the cursor */
  198.  
  199.   while((ch=getc(infile)) != EOF)        /* read one char ahead to detect EOF */
  200.   { ungetc(ch,infile);                   /* put character back in buffer */
  201.     fgets(str,81,infile);                /* get a string of maximal 80+\0 chars */
  202.     len=strlen(str);                     /* calculate it's length */
  203.     if(len==80)                          /* buffer was full without reading a \0 */
  204.       len++;
  205.     Move(rp,xpos,ypos);                  /* move to cursor position */
  206.     Text(rp,str,--len);                  /* output the textline */
  207.     ypos+=8;                             /* 'linefeed' */
  208.     if(ypos==BottomLine)                 /* did we reach the bootom */
  209.     { NextScreen();                      /* yes, do the next screen */
  210.       ypos=17;                           /* cursor back to top */
  211.       if(error==90)                      /* did user want to quit ? */
  212.       { CleanUp();                       /* cleanup and quit */
  213.         goto Quit;
  214.       }
  215.       if(new)                            /* user wants to restart */
  216.       { new=FALSE;                       /* reset flags */
  217.         scndscr=FALSE;
  218.         if(fseek(infile,0L,0L))          /* goto start of file */
  219.           puts("Something wrong with seek");   /* how could this happen ? */
  220.       }
  221.     }
  222.   }
  223.   NextScreen();                          /* clear the last screen */
  224.   CleanUp();                             /* cleanup the theatre */
  225. Quit:
  226.   exit(error);
  227. }
  228.